package com.ctgu.computer.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ctgu.computer.common.CommonConstants;
import com.ctgu.computer.common.UserLocalTread;
import com.ctgu.computer.domain.dto.PageQueryDTO;
import com.ctgu.computer.domain.dto.ReportSendDTO;
import com.ctgu.computer.domain.po.ReportInfo;
import com.ctgu.computer.domain.po.UserInfo;
import com.ctgu.computer.domain.result.PageResult;
import com.ctgu.computer.domain.vo.ImagePaths;
import com.ctgu.computer.exception.BaseException;
import com.ctgu.computer.mapper.ReportMapper;
import com.ctgu.computer.service.ReportService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {

    private final ReportMapper reportMapper;
    private final FileStorageService fileStorageService;
    @Override
    public PageResult getCurrentUserReport(PageQueryDTO pageQueryDTO) {
        Integer userId = UserLocalTread.getUserId();
        PageHelper.startPage(pageQueryDTO.getPageIndex(),pageQueryDTO.getPageSize());
        Page<ReportInfo> result = reportMapper.listByCurrentUser(userId);
        result.forEach(reportInfo -> {
            reportInfo.setImages(JSONObject.parseArray(reportInfo.getImages().toString(), String.class));
        });
        return new PageResult(result.getTotal(),result.getResult());
    }

    @Override
    public void addReportInfo(ReportSendDTO reportSendDTO) {
        ReportInfo reportInfo = new ReportInfo();
        BeanUtils.copyProperties(reportSendDTO,reportInfo);
        Integer userId = UserLocalTread.getUserId();
        reportInfo.setReportSender(userId);
        // 初始设置为未审核状态
        reportInfo.setStatus(null);
/*        MultipartFile[] files = reportSendDTO.getFiles();
        List<String> imagesPath = new ArrayList<>();
        if(files!=null){
            for (MultipartFile file : files) {
                if(!file.isEmpty()){
                    FileInfo upload = fileStorageService.of(file)
                            .setSaveFilename("report" + userId + UUID.randomUUID() + ".png")
                            .upload();
                    imagesPath.add(upload.getUrl());
                }
            }
            reportInfo.setImages(JSONObject.toJSONString(imagesPath));
        }*/
        // 将文件路径数组转化为Json字符串存入数据库
        reportMapper.insert(reportInfo);
    }

    @Override
    public void deleteReportInfo(Integer reportId) {
        Integer userId = UserLocalTread.getUserId();
        ReportInfo reportInfo = reportMapper.selectById(reportId);
        if(!reportInfo.getReportSender().equals(userId)){
            throw new BaseException(CommonConstants.NOT_MAPPING_USER);
        }
        reportMapper.deleteById(reportId);
    }

    @Override
    public ImagePaths upload(MultipartFile... files) {
        List<String> paths = new ArrayList<>();
        for (MultipartFile file : files) {
            if(!file.isEmpty()){
                FileInfo upload = fileStorageService.of(file)
                        .setSaveFilename("report-" +UserLocalTread.getUserId()+ UUID.randomUUID() + ".png")
                        .upload();
                paths.add(upload.getUrl());
            }
        }
        return new ImagePaths(paths);
    }

}
